﻿package Gameplay
{
	import flash.events.MouseEvent;
	import GameUtils.MarkData;
	
	import flash.display.MovieClip;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.Event;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.geom.Matrix;
	import flash.text.TextField;	
	
	public class AbstractTrack extends MovieClip
	{
		protected static const LEFT:uint  = 37;
		protected static const RIGHT:uint = 39;
		protected static const UP:uint    = 38;
		protected static const DOWN:uint  = 40;		
		protected static const A:uint 	  = 65;
		protected static const S:uint     = 83;
		protected static const D:uint     = 68;
		protected static const F:uint     = 70;		
		protected static const SPACE:uint = 32;
		
		protected var FGame:PageGame;
		protected var FStrips:Vector.<FlashStrip>;
		protected var FLeftStrip:FlashStrip;
		protected var FRightStrip:FlashStrip;
		protected var FUpStrip:FlashStrip;
		protected var FDownStrip:FlashStrip;
		protected var FSpaceStrip:FlashStrip;
		protected var FContainer:TrackContainer;		
		protected var FSound:Sound;
		protected var FChannel:SoundChannel;	
		
		protected var FGoodText:TextField;
		protected var FBadText:TextField;
		
		protected var FStatistic:PointsStatisticInfo;
		
		public function AbstractTrack() 
		{
			FStrips = new Vector.<FlashStrip>(5);
			FStrips[1] = getChildByName('FlashLeft') as FlashStrip;
			FStrips[2] = getChildByName('FlashRight') as FlashStrip;
			FStrips[3] = getChildByName('FlashUp') as FlashStrip;
			FStrips[4] = getChildByName('FlashDown') as FlashStrip;
			FStrips[0] = getChildByName('FlashSpace') as FlashStrip;			
			
			for (var i in FStrips)
			{
				FStrips[i].ID = i;
				FStrips[i].Lamp = getChildByName('Lamp' + i) as MovieClip;			
			}
			
			FGoodText = getChildByName('PointsText') as TextField;			
			FBadText = getChildByName('BadText') as TextField;
			FGoodText.mouseEnabled = FBadText.mouseEnabled = false;
		}
		
		public function set GameInstance(AGame:PageGame):void 
		{
			FGame = AGame;
		}
		
		public function Reset():void
		{
			if(FContainer)
			{
				FContainer.y = 0;
				removeChild(FContainer);
			}
				
			FContainer = new TrackContainer();			
			FContainer.mask = getChildByName('ContainerMask') as MovieClip;			
			addChild(FContainer);
			
			swapChildren(FGoodText, FContainer);
			swapChildren(FBadText, FGoodText);
			
			FGoodText.text = '';
			FBadText.visible = false;
			
			FStatistic = new PointsStatisticInfo();
		}
		
		public function set Sound(ASound:Sound):void		
		{
			FSound = ASound;
		}
				
		public function set Data(AData:Vector.<MarkData>):void	
		{
			FContainer.Data = AData;
		}
		
		public function set Height(AHeight:Number):void
		{
			FContainer.Height = AHeight;
		}
		
		public function get Percentage():String
		{
			return (Math.round(FChannel.position / FSound.length * 100)).toString() + '%';
		}
		
		public function KeyDown(ACode:uint):BonusInfo
		{
			var vResult:BonusInfo = new BonusInfo();
			vResult.Points = 0;
			
			switch(ACode)
			{
				case LEFT:
				{
					return PerformKeyPress(1);					
					break;					
				}
				case A:
				{
					return PerformKeyPress(1);					
					break;					
				}
				case DOWN:
				{
					return PerformKeyPress(2);				
					break;
				}
				case S:
				{
					return PerformKeyPress(2);					
					break;
				}
				case RIGHT:
				{
					return PerformKeyPress(3);					
					break;
				}
				case D:
				{
					return PerformKeyPress(3);				
					break;
				}
				case UP:
				{
					return PerformKeyPress(4);			
					break;
				}
				case F:
				{
					return PerformKeyPress(4);				
					break;
				}
				case SPACE:
				{
					return PerformKeyPress(0);
					break;
				}
				default: 
				{
					return vResult;
					break;
				}
			}			
		}
		
		protected function PerformKeyPress(ATrackID:int):BonusInfo
		{			
			return null;
		}	
		
		public function Stop():void
		{
			if(FChannel)
			{
				FChannel.stop();
			}
		}
		
		public function get Statistic():PointsStatisticInfo
		{
			return FStatistic;
		}
	}	
}
